# encoding=utf-8
# author:梁子龙
from jose import jwt

from typing import Union, Any
from datetime import datetime, timedelta

from config import settings

# ACCESS_TOKEN_EXPIRE_MINUTES = 30
ACCESS_TOKEN_EXPIRE_MINUTES = settings.ACCESS_TOKEN_EXPIRE_MINUTES
ALGORITHM = settings.ALGORITHM
JWT_SECRET_KEY = settings.JWT_SECRET_KEY


def create_token(subject: Union[str, Any], expire_delta: int = None) -> str:
    """

    :param subject:
    :param expire_delta:
    :return:
    """
    if expire_delta:
        expire_delta = datetime.utcnow() + expire_delta
    else:
        # 默认时间
        expire_delta = datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
    return jwt.encode({"exp": expire_delta, "sub": str(subject)}, JWT_SECRET_KEY, ALGORITHM)


if __name__ == '__main__':
    print(create_token("阿达的"))
